package org.code.constanQue;
/*
 Bounded Queue
Use any language of your choice. Write a queue that is initialized to
a fixed size. It should not allocate memory after the initialization
(assuming your language can provide that level of granularity).
 */
public class ConstantQue {
	
	private int queue[];
	private int elem_cnt = 0;
	private int push_idx  = 0;
	private int pop_idx = 0;
	
 public ConstantQue(int size) {
		
		queue = new int[size];
		 
	}
	
	public int getSize() {
		return elem_cnt;
	}
	
	public void push(int val) throws Exception {
		if (elem_cnt < queue.length) {
			queue[push_idx] = val;
			push_idx = (push_idx+1) % queue.length;
			elem_cnt++;
		} else {
			throw new Exception("reached queue capacity");
		}
	}
	
	public int pop() throws Exception {
		if (elem_cnt > 0) {
			int elem = queue[pop_idx];			
			pop_idx = (pop_idx+1) % queue.length;
			elem_cnt--;
			return elem;
		} else {
			throw new Exception("No elements in queue");
		}
	}
	
	public static void main(String args[]) {
		try {
			ConstantQue q = new ConstantQue(5);
			q.push(1);
			System.out.println(q.pop());
			q.push(2);
			q.push(3);
			q.push(4);
			System.out.println(q.pop());
			System.out.println(q.pop());
			System.out.println(q.pop());
			System.out.println(q.getSize());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
